﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ScheduleManagement.UI
{
    public partial class frm_Main : Form
    {
        //当前窗体对象
        public static frm_Main Instance;

        public frm_Main()
        {
            
            frm_Login login = new frm_Login();//获取登录窗体实例
            if (login.ShowDialog() != DialogResult.OK)//login.ShowDialog()会进行阻塞，主窗体等待login返回DialogResult结果
            {
                Environment.Exit(Environment.ExitCode);//退出程序
                return;
            }

            InitializeComponent();//构造主窗体

            Instance = this;
        }

        ScheduleManagement.BLL.MainFrom_Bll mainfrmBllOperation = new ScheduleManagement.BLL.MainFrom_Bll();
        BindingSource myBindingSource = new BindingSource(); //绑定数据源 ,创建数据源：BindingSource对象
        BindingSource myBindindSource2 = new BindingSource();
        DataSet myDataSet = new DataSet();

        //全局变量
        bool p_HideFinish;
        bool p_Merge;

        private void frm_Main_Load(object sender, EventArgs e)
        {

            ListSchedule(0, ScheduleManagement.MODEL.Users.U_Id);//网格数据绑定初始化
            dgrdView1.Columns.Add("剩余时间", "剩余时间");
            ListSelect();//查询区数据绑定初台化
            ListAllSchedule();//管理区日程绑定

            #region 窗体控件属性设置
            dtpDate.Format = DateTimePickerFormat.Short;//短日期格式
            dtpTime.Format = DateTimePickerFormat.Time;//时间格式
            dtpTime.ShowUpDown = true;//数字显示、上下选择模式
            dtpDelayed.Format = DateTimePickerFormat.Time;
            dtpDelayed.ShowUpDown = true;


            dgrdView1.RowHeadersVisible = false;//隐藏第一列
            dgrdView1.Columns[1].Frozen = true;//冻结第二列
            dgrdView1.Columns[0].Resizable = DataGridViewTriState.False;//列宽不可调
            dgrdView1.Columns[1].Resizable = DataGridViewTriState.False;//列宽不可调
            dgrdView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;//排序模式
            dgrdView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);//设置列宽为自动调整模式

            dgrdView2.RowHeadersVisible = false;//隐藏第一列
            dgrdView2.Columns[1].Frozen = true;//冻结第二列
            dgrdView2.Columns[0].Resizable = DataGridViewTriState.False;//列宽不可调
            dgrdView2.Columns[1].Resizable = DataGridViewTriState.False;//列宽不可调
            dgrdView2.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;//排序模式
            dgrdView2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);//设置列宽为自动调整模式

            p_HideFinish = chkHideFinish.Checked;
            p_Merge = chkMerge.Checked;

            cboAnnouncement.Items.Add("是");
            cboAnnouncement.Items.Add("否");
            cboCascade.Items.Add("是");
            cboCascade.Items.Add("否");
            cboAddCascade.Items.Add("上级");
            cboAddCascade.Items.Add("并行");

            radAtTime.Checked = true;

            chkRemind.Checked = true;
            chkCascade.Checked = false;

            Remindtimer.Interval = 1000;
            Remindtimer.Enabled = true;

            openFileDialog1.Filter = "音乐文件(*.mp3)|*.mp3|所有文件(*.*)|*.*";
            notifyIcon1.Text = "日程管理系统 测试版";
            notifyIcon1.Visible = false;

            //数据绑定
            cboScheduleQuality.DataBindings.Add("TEXT", myBindindSource2, "属性");
            cboScheduleType.DataBindings.Add("TEXT", myBindindSource2, "日程类型");
            //cboRemind.DataBindings.Add("TEXT",myBindindSource2,"提醒");
            txtVoice.DataBindings.Add("TEXT", myBindindSource2, "提醒音乐");
            txt.DataBindings.Add("TEXT", myBindindSource2, "日程内容");

            comboBox13.Items.Add("日程类型");
            comboBox13.Items.Add("日程状态");
            #endregion

            ShowScheduleInfo();//状态栏信息
            
        }

        


        //网络数据绑定
        private void ListSchedule(int flag,int u_Id) 
        {
            myDataSet = mainfrmBllOperation.List(flag, ScheduleManagement.MODEL.Users.U_Id);
            myBindingSource.DataSource = myDataSet.Tables[0]; //数据源的数据
            dgrdView1.DataSource = myBindingSource;
            //dgrdView1.Columns[dgrdView1.Columns.Count-1].Frozen = true;//冻结第二列
        }

        //显示所有日程信息
        private void ListAllSchedule()
        {
            myDataSet = mainfrmBllOperation.ListAllSchedule();
            myBindindSource2.DataSource = myDataSet.Tables[0];
            dgrdView2.DataSource = myBindindSource2;
        }

        //查询区下拉列表框数据绑定
        private void ListSelect()
        {
            //添加前先把原来的数据清空
            cboRemind.Items.Clear();
            cboSqType.Items.Clear();
            cboStpType.Items.Clear();
            cboStatus.Items.Clear();

            cboScheduleQuality.Items.Clear();
            cboScheduleType.Items.Clear();
            cboIsRemind.Items.Clear();

            //调用BLL层处理请求
            SqlDataReader dr;
            dr = mainfrmBllOperation.List_cboRemind();
            while (dr.Read())
            {
                cboRemind.Items.Add(dr[0]);//查询区提醒类型；
                cboIsRemind.Items.Add(dr[0]);//添加编辑区提醒类型
            }
            dr=mainfrmBllOperation.List_cboSqType();
            while (dr.Read())
            {
                cboSqType.Items.Add(dr[0]);//查询区日程属性
                cboScheduleQuality.Items.Add(dr[0]);//添加编辑区日程属性
            }
            dr = mainfrmBllOperation.List_cboStpType();
            while (dr.Read())
            {
                cboStpType.Items.Add(dr[0]);//查询区日程类型
                cboScheduleType.Items.Add(dr[0]);//添加编辑区日程类型
            }
            dr = mainfrmBllOperation.List_cboStatus();
            while (dr.Read())
            {
                cboStatus.Items.Add(dr[0]);//查询区日程状态                
            }
        }


        private void ShowScheduleInfo()
        {
            //实例化状态栏显示方法
            ScheduleManagement.BLL.StatusInfo_Bll link = new ScheduleManagement.BLL.StatusInfo_Bll();  
          
            //设置状态栏的标签文本，显示当前用户的事务状态
            string ScheduleInfo = link.StatusInfo();
            tlsStatusInfo.Text = ScheduleInfo;

        }

        //合并查询
        private void chkMerge_CheckedChanged(object sender, EventArgs e)
        {
            if (chkMerge.Checked == true)
            {
                
            }
            else
            { 
            }
            p_Merge = chkMerge.Checked;//
        }

        //隐藏完成项
        private void chkHideFinish_CheckedChanged(object sender, EventArgs e)
        {
            if (chkHideFinish.Checked == true)
            {
                groupBox1.Text = "当天待处理日程";
                ListSchedule(1, ScheduleManagement.MODEL.Users.U_Id);//网格数据绑定初始化
            }
            else
            {
                groupBox1.Text = "当天全部日程";
                ListSchedule(0, ScheduleManagement.MODEL.Users.U_Id);//网格数据绑定初始化
            }
            p_HideFinish = chkHideFinish.Checked;
        }

        //按提醒类型查询
        private void cboRemind_SelectedIndexChanged(object sender, EventArgs e)
        {
            myDataSet= mainfrmBllOperation.Select(p_Merge, p_HideFinish, "Remind", cboRemind.SelectedItem.ToString().Trim());
            myBindingSource.DataSource = myDataSet.Tables[0];
        }

        //按日程属性查询
        private void cboSqType_SelectedIndexChanged(object sender, EventArgs e)
        {
            myDataSet= mainfrmBllOperation.Select(p_Merge, p_HideFinish, "SqType", cboSqType.SelectedItem.ToString().Trim());
            myBindingSource.DataSource = myDataSet.Tables[0];
        }

        //按日程类型查询
        private void cboStpType_SelectedIndexChanged(object sender, EventArgs e)
        {
            myDataSet = mainfrmBllOperation.Select(p_Merge, p_HideFinish, "StpType",cboStpType.SelectedItem.ToString().Trim());
            myBindingSource.DataSource = myDataSet.Tables[0];
        }

        //按是否为级联日程查询
        private void cboCascade_SelectedIndexChanged(object sender, EventArgs e)
        {
            myDataSet = mainfrmBllOperation.Select(p_Merge, p_HideFinish, "Cascade", cboCascade.SelectedItem.ToString().Trim());
            myBindingSource.DataSource = myDataSet.Tables[0];
            
        }

        //按权限查询
        private void cboAnnouncement_SelectedIndexChanged(object sender, EventArgs e)
        {
            myDataSet = mainfrmBllOperation.Select(p_Merge, p_HideFinish, "Announ",cboAnnouncement.SelectedItem.ToString().Trim());
            myBindingSource.DataSource = myDataSet.Tables[0];
        }

        //按日程状态查询
        private void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            myDataSet = mainfrmBllOperation.Select(p_Merge, p_HideFinish, "Status",cboStatus.SelectedItem.ToString().Trim());
            myBindingSource.DataSource = myDataSet.Tables[0];
        }

        //定时与延时
        private void radAtTime_CheckedChanged(object sender, EventArgs e)
        {
            if (radAtTime.Checked == true)
            {
                dtpDate.Enabled = true;
                dtpTime.Enabled = true;
                dtpDelayed.Enabled = false;
            }
            else
            {
                dtpDate.Enabled = false;
                dtpTime.Enabled = false;
                dtpDelayed.Enabled = true;
            }
        }

        //添加编辑模块是否提醒
        private void chkRemind_CheckedChanged(object sender, EventArgs e)
        {
            if (chkRemind.Checked == false)
            {
                cboIsRemind.Enabled = false;
            }
            else
            {
                cboIsRemind.Enabled = true;
            }
        }

        private void chkCascade_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCascade.Checked == false)
            {
                cboAddCascade.Enabled = false;
                cboSId.Enabled = false;
            }
            else
            {
                cboAddCascade.Enabled = true;
                cboSId.Enabled = true;
            }
        }

        /// <summary>
        /// 退出选项事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    ScheduleManagement.UI.ClosePoint clopot = new ScheduleManagement.UI.ClosePoint();
        //    clopot.Show();
        //}

        /// <summary>
        /// 执行时间提醒倒计时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Remindtimer_Tick(object sender, EventArgs e)
        {
            this.Text = String.Format("日程管理系统 - {0} - BY 10ATA3 第五小组",DateTime.Now.ToString());
        }

        //
        private void dgrdView1_Click(object sender, EventArgs e)
        {

        }

        private void radAtTime_CheckedChanged_1(object sender, EventArgs e)
        {
            if (radAtTime.Checked == true)
            {
                dtpDate.Enabled = true;
                dtpTime.Enabled = true;
                dtpDelayed.Enabled = false;
            }
            else
            {
                dtpDate.Enabled = false;
                dtpTime.Enabled = false;
                dtpDelayed.Enabled = true;
            }

        }

        private void btnSelectVoice_Click(object sender, EventArgs e)
        {
            switch (openFileDialog1.ShowDialog())
            {
                case DialogResult.Yes:
                case DialogResult.OK:
                    break;
                default:
                    return;
            }
            txtVoice.Text = openFileDialog1.FileName;
        }

        private void btnCancelRemind_Click(object sender, EventArgs e)
        {
            //
            MessageBox.Show("未完成");
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            MessageBox.Show("未完成");
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            MessageBox.Show("未完成");
        }

        private void btnAddSche_Click(object sender, EventArgs e)
        {
            ScheduleManagement.MODEL.Schedule sch = new ScheduleManagement.MODEL.Schedule();
            ScheduleManagement.MODEL.Schedulequality a = new ScheduleManagement.MODEL.Schedulequality();
            a.Sq_Id= "3";
            sch.Schedulequality = a;
            ScheduleManagement.MODEL.Scheduletype b = new ScheduleManagement.MODEL.Scheduletype();
            //b.Stp_Id = mainfrmBllOperation.GetStpId(cboScheduleType.SelectedItem.ToString());
            b.Stp_Id = "5";
            sch.Scheduletype = b;

            sch.S_Content = txt.Text;
            sch.S_Cascade = true;
            ScheduleManagement.MODEL.Remindtype c = new ScheduleManagement.MODEL.Remindtype();
            c.R_Id = "1";
            sch.Remindtype= c;
            ScheduleManagement.MODEL.Status d = new ScheduleManagement.MODEL.Status();
            d.St_Id = "001";
            sch.Status = d;
            sch.S_Voice = txtVoice.Text;
            sch.S_Announcement = true;
            sch.S_Cascade = false;

            mainfrmBllOperation.AddSchedule(sch);
            MessageBox.Show("添加成功");
            ListAllSchedule();//管理区日程绑定
        }

        private void button12_Click(object sender, EventArgs e)
        {
            
        }

        #region 双击托盘上图标时，显示窗体
        /// <summary>
        /// 将窗体从任务栏点击出来的
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                Instance.Show();
                Instance.WindowState = FormWindowState.Normal;
                notifyIcon1.Visible = false;
            }
        }
        #endregion

        #region 点最小化按钮时，最小化到托盘
        private void frm_Main_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                Instance.Hide();
                notifyIcon1.Visible = true;
            }
        }
       #endregion

        private void frm_Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm_ClosePoint closepoint = new frm_ClosePoint();
            if (closepoint.ShowDialog() == DialogResult.OK)//login.ShowDialog()会进行阻塞，主窗体等待login返回DialogResult结果
            {
                //Environment.Exit(Environment.ExitCode);//退出程序
                e.Cancel = true;
            }
            //return;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}